﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using CSCore.CoreAudioAPI;
using CSCore.SoundIn;
using CSCore.Win32;

namespace FM.录音.Services
{
    public class 声卡设备
    {

        public static List<MMDevice> 所有输出声卡()
        {
            var list = new List<MMDevice>();
            using (var deviceEnumerator = new MMDeviceEnumerator())
            using (var deviceCollection = deviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active))
            {
                foreach (var device in deviceCollection)
                {
                    list.Add(device);
                }
            }

            return list.OrderBy(x => x.FriendlyName).ToList();
        }

        public static List<MMDevice> 所有输入声卡()
        {

            var list = new List<MMDevice>();
            using (var deviceEnumerator = new MMDeviceEnumerator())
            using (var deviceCollection = deviceEnumerator.EnumAudioEndpoints(DataFlow.Capture, DeviceState.Active))
            {
                foreach (var device in deviceCollection)
                {
                    list.Add(device);
                }
            }

            return list.OrderBy(x => x.FriendlyName).ToList();
        }

        public static MMDevice 查找输入声卡(string 声卡名称)
        {
            var list = 所有输入声卡();
            var find = list.FirstOrDefault(x => x.FriendlyName == 声卡名称);
            return find ?? MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Capture, Role.Console); ;
        }

        public static MMDevice 查找输出声卡(string 声卡名称)
        {
            var list = 所有输出声卡();
            var find = list.FirstOrDefault(x => x.FriendlyName == 声卡名称);
            return find ?? MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); ;

        }
    }
}
